package com.lime49.lockcrypt.ui;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 * A JPanel with a gradient background
 */
public class GradientJPanel extends JPanel {
    /*private Color color1 = Color.WHITE, 
                  color2 = Color.WHITE;*/
    private Color color1, color2;
    
    public GradientJPanel() {
        super();
        this.setOpaque(true);
    }
    public GradientJPanel(LayoutManager layout) {
        super(layout);
        this.setOpaque(true);
    }
    /**
     * Returns the top color of the gradient
     */
    public Color getColor1() {
        return this.color1;
    }
    /**
     * Returns the top color of the gradient
     * @param color1
     */
    public void setColor1(Color color1) {
        this.color1 = color1;
    }
    /**
     * Returns the bottom color of the gradient
     */
    public Color getColor2() {
        return this.color2;
    }
    /**
     * Returns the bottom color of the gradient
     * @param color2
     */
    public void setColor2(Color color2) {
        this.color2 = color2;
    }
    /**
     * Sets the colour of the gradient (top to bottom)
     * @param color1 The top colour
     * @param color2 The bottom colour
     */
    public void setGradient(Color color1, Color color2) {
        this.color1 = color1;
        this.color2 = color2;
        repaint();
    }
    
    @Override
    public Insets getInsets() {
        return new Insets(2,15,2,15);
    }
    
    @Override
    public void setBorder(Border border) {
        super.setBorder(border);
    }
    
    @Override
    public void paintComponent(Graphics g) {
        if(!isOpaque()) {
            super.paintComponent( g );
            return;
        }
        Graphics2D g2d = (Graphics2D)g;
        int w = getWidth( );
        int h = getHeight( );
        if(color1 != null && color2 != null) {
            // Paint a gradient from top to bottom
            GradientPaint gp = new GradientPaint(
                0, 0, color1,
                w, 0, color2);

            g2d.setPaint( gp );
            g2d.fillRect( 0, 0, w, h );
        }
    
 
        setOpaque( false );
        super.paintComponent( g );
        setOpaque( true );
    }
}
